# ======================================================================
#
# Copyright (C) ADN
# ADN::LIBRARY::SNMP::Snmpv1 Snmpv1.pm
# Description:
#  - Requetes SNMP V1 
#
# ======================================================================

package ADN::LIBRARY::SNMP::Snmpv1;

use strict;
use Net::SNMP;
use ADN::CONF::Read;

# #######################
# Création de l'objet self + init. variables
# #######################
sub new
{
  	# Construction de l'objet $self
	  my $self = shift;
	  my $class = ref($self) || $self;
	  return $self if ref $self;
	  $self = bless {} => $class;
    ($self->{SYSTEM}) = @_;
    return ($self);
}

# #######################
# Fonction principale 1
# #######################
sub start
{
   my $self = shift;
   ($self->{session}) = @_;
   my ($result, $k0, $v0, $kb, $vb);
   @{$self->{oids}} = values %{$self->{SYSTEM}};

   $self->baseoid();
   $result = $self->{session}->get_request( -varbindlist => \@{$self->{varlist}} );
   while (($k0, $v0) = each %{$result})
   {
     while (($kb, $vb) = each %{$self->{SYSTEM}})
     {
       if ($vb =~ m/^$k0$/smx)
       {
         $self->{result}{$kb} = $v0;
       }
     }
   }
   undef $result;
   undef $k0;
   undef $v0;
   undef $kb;
   undef $vb;
   $self->start2();
   $self->verif();
   $self->{session}->close;
   return (\%{$self->{result}});
}

# #######################
# Fonction verification SNMP
# #######################
sub verif
{
  my $self = shift;
  my @verif = keys %{$self->{result}};

  if (scalar @verif == 0)
  {
    $self->{result}{Erreur} = '1';
  }
  else
  {
    $self->{result}{Erreur} = '0';
  }
  undef @verif;
  return;
}


# #######################
# Fonction principale 2
# #######################
sub start2
{
   my $self = shift;
   my ($cnt, $result, $k, $v1);
   my @tab;

   foreach my $ligne (@{$self->{baseoid}})
   {
      $cnt = 1;
      $result = $self->{session}->get_table( -baseoid => $ligne );
      if (defined $result)
      {
        while (($k, $v1) = each %{$self->{SYSTEM}})
        {
          if ($v1 =~ m/^$ligne$/smx)
          {
            @tab = values %{$result};
            @tab = reverse @tab;
            $self->{result}{$k} = scalar @tab;
            foreach my $v2 (@tab)
            {
              $self->{result}{$k.".".$cnt} = $v2;
              $cnt++;
            }
          }
        }
      }
   }
   undef $k;
   undef $v1;
   undef $cnt;
   undef $result;
   undef @tab;
   return;
}

# #######################
# Fonction preparation des tableaux
# => types requetes
# #######################
sub baseoid
{
  my $self = shift;

  foreach my $ligne (@{$self->{oids}})
  {
    if ($ligne =~ m/\.0$/smx)
    {
      push @{$self->{varlist}}, $ligne;
    }
    else
    {
      push @{$self->{baseoid}}, $ligne;
    }
  }
	return;
}

# #######################
# DESTROY OBJECT
# #######################
sub DESTROY {
    my $self = shift;
    return;
}
1;
__END__